<?php namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\UserInfo
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property int $photo_id 用户头像
 * @property string|null $nickname 用户昵称
 * @property string|null $realname 真实姓名
 * @property int $sex 性别，0：保密，1:男，2:女
 * @property string|null $id_card 身份证号
 * @property string|null $province 省份
 * @property string|null $city 城市
 * @property string|null $area 地区
 * @property string|null $address 详细地址
 * @property int $integral 积分
 * @property string|null $birthday 生日
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\UserInfo onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereBirthday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereIdCard($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereIntegral($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo wherePhotoId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereRealname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\UserInfo withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\UserInfo withoutTrashed()
 * @mixin \Eloquent
 * @property string|null $company_name 单位名称
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereCompanyName($value)
 * @property int $score 分数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\UserInfo whereScore($value)
 */
class UserInfo extends Model
{
    protected $table = "user_info";
    use SoftDeletes;
    public static function getUserInfo($user_id){
        $data=self::whereUserId($user_id)->first();
        if ($data){
            $data->photo_url=FileManager::getImgUrl($data->photo_id);
            $data->sex_name =$data->sex == 0 ? '保密' : ($data->sex == 1 ? '男' : '女');
        }

        return $data;
    }

    public static function getStaffName($staff_id){
        $self = Staff::whereId($staff_id)->first();
        if ($self){
            $staffName = $self->realname;
            if(!$staffName){
                $data=self::whereUserId($self->user_id)->first();
                if ($data){
                    $staffName =  $data->realname;
                }
            }

            return $staffName;
        }

        return '';
    }

    public static function getStaffPhotoUrl($staff_id){
        $self = Staff::whereId($staff_id)->first();
        if ($self){
            $data=self::whereUserId($self->user_id)->first();
            if ($data){
                return FileManager::getImgUrl($data->photo_id);
            }
        }
        return '';
    }

    /**
     * @param $user_id
     * @return int
     */
    public static function getUserIntegral($user_id){
        $data=self::whereUserId($user_id)->first();
        return $data ? (int)$data->integral : 0;
    }
}
